package tgc.edu.szxt.demo.web.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.szxt.demo.custom.AjaxResult;
import tgc.edu.szxt.demo.custom.TablePageable;
import tgc.edu.szxt.demo.entity.Course;
import tgc.edu.szxt.demo.entity.Grade;
import tgc.edu.szxt.demo.entity.Student;
import tgc.edu.szxt.demo.entity.SysUser;
import tgc.edu.szxt.demo.entity.TbClass;
import tgc.edu.szxt.demo.security.UserUtils;
import tgc.edu.szxt.demo.service.CourseService;
import tgc.edu.szxt.demo.service.GradeService;
import tgc.edu.szxt.demo.service.StudentService;
import tgc.edu.szxt.demo.service.TbClassService;
import tgc.edu.szxt.demo.web.form.CourseForm;
import tgc.edu.szxt.demo.web.form.StudentForm;

@Controller
@RequestMapping(value = "/grade")
public class GradeController {
	
	@Autowired
	private GradeService gradeService;
	@Autowired
	private TbClassService tbClassService;
	@Autowired
	private CourseService CourseService;
	@Autowired
	private StudentService studentService;
	@Autowired    
	private UserUtils userUtils;
	
	
	
	@RequestMapping(value = "/peasonGrade")
	public String peasonGrade(ModelMap modelMap) {
		SysUser user = userUtils.getUser();
		Integer id = user.getId();
		List<Grade> list =gradeService.findByStudentId(id);
		System.out.println(list);
		modelMap.put("list", list);
		return "grade/peasonGrade";
	}
	
	
	@RequestMapping(value = "/peasonId")
	@ResponseBody
	public Object peasonId(ModelMap modelMap) {
		SysUser user = userUtils.getUser();
		Integer id = user.getId();
		return id;
	}
	
	@RequestMapping(value = "/peasonBjId")
	@ResponseBody
	public Object peasonBjId(ModelMap modelMap) {
		SysUser user = userUtils.getUser();
		Integer id = user.getId();
		Student stu = studentService.findById(id);
		Integer bjId = stu.getTbClass().getId();
		return bjId;
	}
	
	
	
	@RequestMapping(value = "/peasonGradeCjb")
	public void peasonGradeCjb(ModelMap model) {
		SysUser user = userUtils.getUser();
		Integer id = user.getId();
		Student stu = studentService.findById(id);
		TbClass bj = stu.getTbClass();
		Integer bjId = bj.getId();
		List<Course> kcs = gradeService.findDistinctCourseByTbClassId(bjId);
		List<Object[]> cjb = gradeService.findByCjb(bjId, kcs);
		model.put("kcs", kcs);
		model.put("cjb", cjb);
	}
	
	
	//-----------------------------班级成绩录入---------------------------//
	@RequestMapping(value = "/classGrade")
	public void classGrade(ModelMap model) {
		List<TbClass> bjs = tbClassService.findAll();
		List<Course> kcs = CourseService.findAll();
		model.put("bjs",bjs);
		model.put("kcs",kcs);
	}
	@RequestMapping(value = "/classCj")
	public String classCj(Integer bjId,Integer kcId,ModelMap model) {
		List<Student> stus = studentService.findByTbClassId(bjId);
		List<Grade> grades= new ArrayList<>();
		for (Student student : stus) {
			Grade grade = gradeService.findByStudentIdAndCourseId(student.getId(),kcId);
			if(grade==null) {
				Student stu = studentService.findById(student.getId());
				Grade grade1=new Grade();
				grade1.setStudent(stu);
				Course course = CourseService.findById(kcId);
				grade1.setCourse(course);
				grade1.setGrade(0);
				gradeService.save(grade1);
				grades.add(grade1);
			}else{
				grades.add(grade);}
		}
		return "grade/classCjTable";
	}
	
	
	@RequestMapping(value = "/classCjxg")
	@ResponseBody
	public Object classCjxg(Integer id,Integer grade) {
		Grade cj = gradeService.findById(id);
		cj.setGrade(grade);
		try {
			gradeService.save(cj);
			return  new AjaxResult("修改成功！");
		} catch (Exception e) {
			return  new AjaxResult(false,"修改失败！");
		}

	}
	
	
	@SuppressWarnings("serial")
	public Specification<Grade> buildSpec(Integer bjId,Integer kcId) {
		Specification<Grade> spec=new Specification<Grade>() {

			@Override
			public Predicate toPredicate(Root<Grade> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				if(bjId!=null) {
					rules.add(cb.equal(root.get("student").get("tbClass").get("id").as(String.class), bjId));
				}
				if(kcId!=null) {
					rules.add(cb.equal(root.get("course").get("id").as(String.class), kcId));
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}
	@RequestMapping(value = "/page")
	@ResponseBody
	public Object page(TablePageable tablePaegable,Integer bjId,Integer kcId) {
		List<Student> students=studentService.findByTbClassId(bjId);
		for(Student s:students) {
		Grade grade=gradeService.findByStudentIdAndCourseId(s.getId(), kcId);
		if(grade==null) {
			Grade grade1=new Grade();
			grade1.setCourse(CourseService.findById(kcId));
			grade1.setStudent(studentService.findById(s.getId()));
			grade1.setGrade(null);
			gradeService.save(grade1);
			}
		}
		PageRequest pageable = tablePaegable.buildPageRequest();
		Specification<Grade> spec = buildSpec(bjId,kcId);
		Page<Grade> page = gradeService.findAll(spec, pageable);
		return AjaxResult.buildTableResult(page);
	}
	
	
	//-----------------------------个人成绩录入---------------------------//
	@RequestMapping(value = "/xsClassGrade")
	public void xsClassGreade(ModelMap model) {
		List<TbClass> bjs = tbClassService.findAll();
		model.put("list",bjs);
	}
	
	@RequestMapping(value = "/xsClassCjTable")
	public void xsClassCjTable(Integer bjId,ModelMap model) {
		List<Student> stus = studentService.findByTbClassId(bjId);
		model.put("stus", stus);
	}
	
	@RequestMapping(value = "/xsCjTable")
	public void xsCjTable(Integer xsId,ModelMap model) {
		List<Grade> kcCj = gradeService.findByStudentId(xsId);
		model.put("kcCj",kcCj);
	}
	
	@RequestMapping(value = "/xsClassXg")
	@ResponseBody
	public Object xsClassXg(Integer id,Integer grade) {
		Grade cj = gradeService.findById(id);
		cj.setGrade(grade);
		gradeService.save(cj);
		return "OK";
	}
	
	
	
	@SuppressWarnings("serial")
	public Specification<Grade> buildSpec1(Integer bjId,Integer xsId) {
		Specification<Grade> spec=new Specification<Grade>() {

			@Override
			public Predicate toPredicate(Root<Grade> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				if(bjId!=null) {
					rules.add(cb.equal(root.get("student").get("tbClass").get("id").as(String.class), bjId));
				}
				if(xsId!=null) {
					rules.add(cb.equal(root.get("student").get("id").as(String.class), xsId));
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}
	
	@RequestMapping(value = "/page2")
	@ResponseBody
	public Object page2(TablePageable tablePaegable,Integer bjId,Integer xsId) {
		PageRequest pageable = tablePaegable.buildPageRequest();
		Specification<Grade> spec = buildSpec1(bjId,xsId);
		Page<Grade> page = gradeService.findAll(spec, pageable);
		return AjaxResult.buildTableResult(page);
	}
	
	
	//-----------------------------班级成绩查询---------------------------//
	@RequestMapping(value = "/allGrade")
	public void allGrade(ModelMap model) {
		List<TbClass> bjs = tbClassService.findAll();
		model.put("bjs",bjs);
	}
	
	
	@RequestMapping("/bjCjb")
	public String bjCjb(Integer bjId,ModelMap model) {
		List<Course> kcs = gradeService.findDistinctCourseByTbClassId(bjId);
		List<Object[]> cjb = gradeService.findByCjb(bjId, kcs);
		model.put("kcs", kcs);
		model.put("cjb", cjb);
		return "grade/cjb";
	}
	
	//------------------------------------------------------------//

}
